package com.hai.video;

import android.media.MediaPlayer;
import android.view.Surface;
import android.view.SurfaceHolder;

import java.io.IOException;

public class MyPlayer implements IMediaApi {
    MediaPlayer mediaPlayer;

    public MyPlayer() {
        mediaPlayer = new MediaPlayer();
    }

    @Override
    public void setOnCompletionListener(MediaPlayer.OnCompletionListener listener) {
        mediaPlayer.setOnCompletionListener(listener);
    }

    @Override
    public void setOnErrorListener(MediaPlayer.OnErrorListener listener) {
        mediaPlayer.setOnErrorListener(listener);
    }

    @Override
    public void setOnInfoListener(MediaPlayer.OnInfoListener listener) {
        mediaPlayer.setOnInfoListener(listener);
    }

    @Override
    public void setOnPreparedListener(MediaPlayer.OnPreparedListener listener) {
        mediaPlayer.setOnPreparedListener(listener);
    }

    @Override
    public void setOnSeekCompleteListener(MediaPlayer.OnSeekCompleteListener listener) {
        mediaPlayer.setOnSeekCompleteListener(listener);
    }

    @Override
    public void setOnVideoSizeChangedListener(MediaPlayer.OnVideoSizeChangedListener listener) {
        mediaPlayer.setOnVideoSizeChangedListener(listener);
    }

    @Override
    public void setDataSource(String path) {
        try {
            mediaPlayer.setDataSource(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void seekTo(long position) {
        mediaPlayer.seekTo(Long.valueOf(position).intValue());
    }

    @Override
    public void prepare() {
        mediaPlayer.prepareAsync();
    }

    @Override
    public void start() {
        mediaPlayer.start();
    }

    @Override
    public void pause() {
        mediaPlayer.pause();
    }

    @Override
    public void stop() {
        mediaPlayer.stop();
    }

    @Override
    public void reset() {
        mediaPlayer.reset();
    }

    @Override
    public void release() {
        mediaPlayer.release();
    }

    @Override
    public boolean isPlaying() {
        return mediaPlayer.isPlaying();
    }

    @Override
    public boolean isPaused() {
        return mediaPlayer.getCurrentPosition() > 0 && mediaPlayer.getVideoWidth() > 0 && !isPlaying();
    }

    @Override
    public int getVideoWidth() {
        return mediaPlayer.getVideoWidth();
    }

    @Override
    public int getVideoHeight() {
        return mediaPlayer.getVideoHeight();
    }

    @Override
    public long getProgress() {
        return mediaPlayer.getCurrentPosition();
    }

    @Override
    public long getDuration() {
        return mediaPlayer.getDuration();
    }

    @Override
    public void setDisplay(SurfaceHolder holder) {
        mediaPlayer.setDisplay(holder);
    }

    @Override
    public void setSurface(Surface surface) {
        mediaPlayer.setSurface(surface);
    }

    @Override
    public Object getMedia() {
        return mediaPlayer;
    }
}
